﻿using EveryBookies.Common;
using EveryBookies.Model;
using DataServices.Services;
using EveryBookies;
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Windows.UI.Core;


namespace EveryBookies.ViewModel
{
    public class BookiesViewModel : ViewModelBase, IBookiesViewModel
    {
        bool _isShowOfferLoading = true;
        public bool IsShowOfferLoading
        {
            get
            {
                return _isShowOfferLoading;
            }
            set
            {
                Set(ref _isShowOfferLoading, value);
            }
        }

        bool _isShowBookiesLoading = true;
        public bool IsShowBookiesLoading
        {
            get
            {
                return _isShowBookiesLoading;
            }
            set
            {
                Set(ref _isShowBookiesLoading, value);
            }
        }

        bool _isShowFavoriteLoading = true;
        public bool IsShowFavoriteLoading
        {
            get
            {
                return _isShowFavoriteLoading;
            }
            set
            {
                Set(ref _isShowFavoriteLoading, value);
            }
        }

        ObservableCollection<Offer> favourites;
        public ObservableCollection<Offer> Favourites
        {
            get
            {
                if (favourites == null)
                {
                    favourites = new ObservableCollection<Offer>();
                }
                return favourites;
            }
            set
            {
                Set(ref favourites, value);
            }
        }
        ObservableCollection<Booky> listBookies;
        public ObservableCollection<Booky> Bookies
        {
            get
            {
                if (listBookies == null)
                {
                    listBookies = new ObservableCollection<Booky>();
                }
                return listBookies;
            }
            set
            {
                Set(ref listBookies, value);
            }
        }

        ObservableCollection<Offer> listOffers;
        public ObservableCollection<Offer> Offers
        {
            get
            {
                if (listOffers == null)
                {
                    listOffers = new ObservableCollection<Offer>();
                }
                return listOffers;
            }
            set
            {
                Set(ref listOffers, value);
            }
        }

        ObservableCollection<Banner> listBanner;
        public ObservableCollection<Banner> Banners
        {
            get
            {
                if (listBanner == null)
                {
                    listBanner = new ObservableCollection<Banner>();
                }
                return listBanner;
            }
            set
            {
                Set(ref listBanner, value);
            }
        }
        IRestfullServices restfullServices;


        public BookiesViewModel(IRestfullServices restfullServices)
        {
            this.restfullServices = restfullServices;
            restfullServices.SetBaseAddress("http://www.everybookie.co.uk.gridhosted.co.uk");
            // GetBookies(App.ItemsCountDefault.ToString(), "0");
            GetOffer(App.ItemsCountDefault.ToString(), "0");
        }
        public async Task GetOffer(string limit, string offset)
        {
            try
            {
                if (!App.InternetConnection())
                {
                    MessageBox.Show("Network not found, please check network connection");
                    return;
                }
                IsShowOfferLoading = true;
                var url = string.Format("/betapp/api/get_offers?limit={0}&offset={1}&device_id={2}", limit, offset, App.UniqueDevice);
                var getDataOffer = await restfullServices.PostAsync(url, null);
                if (getDataOffer == "No Content")
                {
                    IsShowOfferLoading = false;
                    return;
                }
                if (getDataOffer != null)
                {
                    var paserData = JsonSerialization.s_Deserialize<OffersData>(getDataOffer);
                    if (paserData != null)
                    {
                        if (paserData.offers.Count < App.ItemsCountDefault && paserData.offers.Count > 0)
                        {
                            foreach (var m in paserData.offers)
                            {
                                Offers.Add(m);
                            }
                        }
                        else if (paserData.offers.Count == 0)
                        {
                            IsShowOfferLoading = false;
                            return;
                        }
                        if (Offers.Count == 0)
                        {
                            Offers = new ObservableCollection<Offer>(paserData.offers);
                            
                        }
                        else if (paserData.offers.Count == App.ItemsCountDefault)
                        {
                            foreach (var m in paserData.offers)
                            {
                                Offers.Add(m);
                            }
                        }
                        if(offset=="0")
                            HandleBanner(paserData.banner, paserData.favorite);
                    }
                }
                else
                {
                    MessageBox.Show("Get data failed, please try again");
                }

            }
            catch (Exception ex)
            {
                IsShowOfferLoading = false;
            }
            IsShowOfferLoading = false;
        }
        private void HandleBanner(List<Banner> banner, List<Favorite> favorite)
        {
            Banners.Clear();
            if (banner.Count > 0)
            {
                if (favorite.Count > 0)
                {
                    foreach (var d in banner)
                    {
                        var getData = favorite.FirstOrDefault(m => m.title == d.name);
                        if (getData == null)
                        {
                            Banners.Add(d);
                        }
                    }
                }
                else
                {
                    Banners = new ObservableCollection<Banner>(banner);
                }
            }
        }

        public async Task GetBookies(string limit, string offset)
        {
            if (!App.InternetConnection())
            {
                MessageBox.Show("Network not found, please check network connection");
                return;
            }
            IsShowBookiesLoading = true;
            var url = string.Format("/betapp/api/get_bookies?limit={0}&offset={1}&device_id={2}", limit, offset, App.UniqueDevice);
            var getDataOffer = await restfullServices.PostAsync(url, null);
            if (getDataOffer == "No Content")
            {
                IsShowBookiesLoading = false;
                return;
            }
            if (getDataOffer != null)
            {
                var paserData = JsonSerialization.s_Deserialize<BookiesData>(getDataOffer);
                if (paserData != null)
                {
                    if (paserData.bookies.Count < App.ItemsCountDefault && paserData.bookies.Count > 0)
                    {
                        foreach (var m in paserData.bookies)
                        {
                            Bookies.Add(m);
                        }

                    }
                    else if (paserData.bookies.Count == 0)
                    {
                        IsShowBookiesLoading = false;
                        return;
                    }
                    if (Bookies.Count == 0)
                    {
                        Bookies = new ObservableCollection<Booky>(paserData.bookies);
                    }
                    else if (paserData.bookies.Count == App.ItemsCountDefault)
                    {
                        foreach (var m in paserData.bookies)
                        {
                            Bookies.Add(m);
                        }
                    }
                    if (offset == "0")
                        HandleBanner(paserData.banner, paserData.favorite);
                }
            }
            else
            {
                MessageBox.Show("Get data failed, please try again");
            }
            IsShowBookiesLoading = false;
        }

        public async Task<bool> SetFavorites(string bookie_id, string flag)
        {
            if (!App.InternetConnection())
            {
                MessageBox.Show("Network not found, please check network connection");
                return false;
            }

            bool isResult = false;
            IsShowBookiesLoading = true;
            var url = string.Format("/betapp/api/set_favorite?bookie_id={0}&device_id={1}&flg={2}", bookie_id, App.UniqueDevice, flag);
            var setFavorite = await restfullServices.PostAsync(url, null);
            if (setFavorite != null)
            {
                var paserData = JsonSerialization.s_Deserialize<SetFavorites>(setFavorite);
                if (paserData.success == 1)
                {
                    isResult = true;
                    Bookies.Clear();
                    GetBookies(App.ItemsCountDefault.ToString(), "0");
                }
                else
                {
                    MessageBox.Show(paserData.msg);
                    isResult = false;
                }

            }
            else
            {
                MessageBox.Show("Get data failed, please try again");
                isResult = false;
            }

            IsShowBookiesLoading = false;
            return isResult;
        }

        public async Task GetFavorites()
        {
            if (!App.InternetConnection())
            {
                MessageBox.Show("Network not found, please check network connection");
                return;
            }
            IsShowFavoriteLoading = true;
            var url = string.Format("/betapp/api/get_favorite_list?device_id={0}", App.UniqueDevice);
            var setFavorite = await restfullServices.PostAsync(url, null);
            if (setFavorite != null)
            {
                var paserData = JsonSerialization.s_Deserialize<FavoriteData>(setFavorite);
                if (paserData != null && paserData.offers != null)
                {
                    Favourites = new ObservableCollection<Offer>(paserData.offers);
                }
                else if(setFavorite.Contains("msg"))
                {
                    var paserDatas = JsonSerialization.s_Deserialize<SetFavorites>(setFavorite);
                    MessageBox.Show(paserDatas.msg);
                }
                else
                {
                    MessageBox.Show("No offers in favourites");
                }
            }
            else
            {

                MessageBox.Show("Get data failed, please try again");
            }
            IsShowFavoriteLoading = false;
        }
    }
}